/**   
 * @Title: DictionaryServiceImpl.java 
 * CopyRight(c)2010-DMX 
 */
package com.dmx.fw.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;

import com.dmx.fw.bean.mapping.pojo.CacheCode;
import com.dmx.fw.dao.interfaces.JdbcDao;
import com.dmx.fw.service.interfaces.DictionaryService;

/** 
 * @Description: 数据字典Service实现
 * @author: system
 * @date: 2006
 * @version V1.0 
 * 
 */
public class DictionaryServiceImpl extends BaseServiceImpl implements DictionaryService{
	
	private JdbcDao dao = null;


    private static Logger log = Logger.getLogger(DictionaryServiceImpl.class);

    /** 
	 * @Fields _cacheCodeMap: 缓存cacheCode的配置信息
	 */
    private static final HashMap _cacheCodeMap = new HashMap();
    

	public void setJdbcDao(JdbcDao jdbcDao) {
		this.dao = jdbcDao;
	}
    
    public List getDataListByCacheCode(String cacheKey) {
    	return getDataListByCacheCode(cacheKey,null);
    }
    
    /**
     * 根据cacheCode获得字典数据列表，如果cach_code表没有配置，则根据字典的TYPE_CODE进行值的读取
     * @param code
     * @param params
     * @return List
     */
    public List getDataListByCacheCode(String cacheKey,List params) {
    	CacheCode cacheCode = getCacheCodeInfo(cacheKey);
    	
    	cacheKey = cacheKey.toUpperCase();
        List returnList = new ArrayList();
        
        //如果带参数则直接从数据库查询
        if (params!=null && params.size()>0){
        	String strSQL =  cacheCode.getListSql();
        	for (int i = 0;i < params.size(); i++){
	        	 int beign=StringUtils.indexOf(strSQL, "?");
	        	 if(beign>-1){
		       		 int endClose=StringUtils.indexOf(strSQL, "]",beign);
		       		 int endOpen=StringUtils.lastIndexOf(strSQL, "[",beign);
		       		 String oldParm="";
		       		 String newParm="";
		       		 String value=(String)params.get(i);
		       		 if(endOpen>-1 && endClose>-1){
		       			oldParm=StringUtils.substring(strSQL, endOpen, endClose+1);
		       			newParm=StringUtils.isNotBlank(value)? StringUtils.replaceOnce(StringUtils.replaceOnce(StringUtils.replaceOnce(oldParm, "?",value),"[",""),"]",""):"1=1";
		       		 }else{
		       			oldParm="?";
		       			newParm=value;
		       		 }
	       		     strSQL = StringUtils.replaceOnce(strSQL,oldParm,newParm);
	        	 }
        		//strSQL = StringUtils.replaceOnce(strSQL, "?",(String)params.get(i));
        	}
        	//System.out.println("catche"+strSQL);
        	return dao.queryData(strSQL);
        	
        }

 /*       
        if ("1".equals(cacheCode.getCached())) {  //如果设置需要缓存

	        try {
	            // Get from the cache
	            returnList = JBossCacheUtils.getDataListByCacheKey(cacheKey);
	        } catch (Exception e) {
	        		//e.printStackTrace();
	                // Get the value (probably from the database)
	                returnList = dao.queryData(cacheCode.getListSql());
	                // Store in the cache
	                if (returnList != null) {
	                    JBossCacheUtils.putDataListByCacheKey(cacheKey, returnList);
	                }
	               
	          
	        }finally{
	        	 return returnList;
	        }

        } else {
        	returnList = dao.queryData(cacheCode.getListSql());
        }
*/        
       returnList = dao.queryData(cacheCode.getListSql());
       return returnList;
    }
    
    
    
    /**
     * 根据dictpk查找，该字典所影响的cachecode，用于在字典数据修改时，刷新缓存
     * @param cacheKey
     * @return
     */
//    public void flushCacheByCacheCode(String cacheKey) {
//    	CacheCode cacheCode = getCacheCodeInfo(cacheKey);
//    	List returnList = dao.queryData(cacheCode.getListSql());
//    	 if (returnList != null) {
//             JBossCacheUtils.putDataListByCacheKey(cacheKey, returnList);
//         }
//    }
    
  public void flushCacheByCacheCode(String cacheKey) {
}    

	
	
    /**
     * 获得cachecode的配置信息,无法从cach_code 获得信息，构造一个CacheCode对象，从pram_data读取数据
     * @param code
     * @return
     */
    private CacheCode getCacheCodeInfo(String code) {
    	CacheCode cacheCode = (CacheCode) _cacheCodeMap.get(code);

        if (cacheCode == null) {
            //从数据库中获得dictMgrHead
        	List list = dao.find("from CacheCode where code='"+ code +"'");
        	if (list!=null && list.size()>0){
        		 cacheCode = (CacheCode)list.get(0);
        	}
        	
        	//无法从cach_code 获得信息，构造一个CacheCode对象，从pram_data读取数据
        	if (cacheCode==null){
        		cacheCode = new CacheCode();
        		cacheCode.setCode(code);
        		
        		cacheCode.setConfigType(CacheCode.CONFIG_TYPE_parm_data);
        		
        		cacheCode.setCached("1");  //设置缓存，在更新字典表时，需要更新cachcode=字典type_code 和param_code = 字典type_code的缓存
            		
        		
        		cacheCode.setListSql("select code,name from SYSTEM_PARM_DATA where type_code='" + code + "' and status=1  order by sortnum");		
        	}
        	
        	if (cacheCode!=null){
	            synchronized (_cacheCodeMap) {
	                _cacheCodeMap.put(code, cacheCode);
	            }
        	}
        }
        return cacheCode;
    }

}
